package com.woniu.tool;

import com.woniu.config.MailConfig;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;

/**
 * 2022-9-22 15:44:50
 * Wei.SJ
 */
@Setter
@Getter
@ToString
@Component
@ConfigurationProperties(prefix = "spring.mail")
public class MailProperties {
    @Autowired
    private MailConfig mailConfig;

    private String host;

    private String username;

    private String password;

    private String from;

    private Integer port;

    @Value("${spring.mail.properties.mail.smtp.auth}")
    private Boolean auth;

    @Value("${spring.mail.properties.mail.smtp.starttls.enable}")
    private Boolean starttlsEnable;

    @Value("${spring.mail.properties.mail.smtp.ssl.trust}")
    private String sslTrust;

    /**
     * 2022-9-22 15:52:47
     * Wei.SJ
     *
     * @param to      接收者QQ号
     * @param subject 主题
     * @param text    消息体
     */
    public void sendMail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        // 邮件发送人
        message.setFrom(mailConfig.getFrom());
        // 邮件接收人（可以使用 String[] 发送给多个用户）
        message.setTo(to);
        // 邮件标题
        message.setSubject(subject);
        // 邮件内容
        message.setText(text);
        // 发送邮件
        mailConfig.getMailSender("513521187@qq.com", "qrtwejarmklccaci").send(message);
    }
}
